package com.millstein.exception;

import com.millstein.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description:
 * @author: tsing
 * @create: 2021-09-09 20:52
 */
@Slf4j
@RestControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(Exception.class)
    public JsonData handler(Exception e) {

        if (e instanceof BizException) {
            log.error("[业务异常{}]", e.getMessage(), e);
            BizException bizException = (BizException) e;
            return JsonData.buildCodeAndMsg(bizException.getCode(), e.getMessage());
        } else {
            log.error("[非业务异常{}]", e.getMessage(), e);
            return JsonData.buildError("系统异常");
        }
    }
}
